/*
 ============================================================================
 Name		: SCOServerEngine.cpp
 Author	  : William Zhang
 Version	 : 1.0
 Copyright   : all rights reserved.
 Description : CSCOServerEngine implementation
 ============================================================================
 */
#include <bttypes.h>
#include "SCOServerEngine.h"
#include "Utils.h"


CSCOServerEngine::CSCOServerEngine()
	{
	// No implementation required
	}

CSCOServerEngine::~CSCOServerEngine()
	{
	if(iSCOLink)
		{
		delete iSCOLink;
		iSCOLink = NULL;
		}
	
	if(iPhyLink)
		{
		delete iPhyLink;
		iPhyLink = NULL;
		}
	
	iSockServ.Close();
	}

CSCOServerEngine* CSCOServerEngine::NewLC()
	{
	CSCOServerEngine* self = new (ELeave) CSCOServerEngine();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CSCOServerEngine* CSCOServerEngine::NewL()
	{
	CSCOServerEngine* self = CSCOServerEngine::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CSCOServerEngine::ConstructL()
	{
	TInt err;
	
	//the MAC address of remote device
	iAddr.SetReadable(_L("002669e4bf4c"));  //N85's address
	
	User::LeaveIfError(iSockServ.Connect());
	
	iPhyLink = CBluetoothPhysicalLinks::NewL(*this,iSockServ);
	iSCOLink = CBluetoothSynchronousLink::NewL(*this,iSockServ);
	
	err = iPhyLink->CreateConnection(iAddr);
	if(err != KErrNone)
		{
		//error
		User::LeaveIfError(err);
		}
	
	}

void CSCOServerEngine::Send(const TDesC& aMsg)
	{
	TBuf8<50> msg;
	msg.Append(aMsg);
	iSCOLink->Send(msg);
	}

//===============From MBluetoothSynchronousLinkNotifier==================
// 
// connection complete handler
//
void CSCOServerEngine::HandleSetupConnectionCompleteL(TInt aErr)
	{
	
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Setup succeed"));
     
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Setup faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }
	}

//
// new connection accept handler
//
void CSCOServerEngine::HandleAcceptConnectionCompleteL(TInt aErr)
	{
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Accept succeed"));
        
        //call receive function here        
        TInt err = iSCOLink->Receive(iMsg);
        if(err != KErrNone)
        	{
            TBuf<50> buf;
            buf.AppendFormat(_L("Start receive faild,err:%d"),aErr);
            CUtils::ShowMsg(buf);
        	}
//        else
//        	{
//        	CUtils::ShowMsg(_L("receiving..."));
//        	}
        User::After(15000000);
        TBuf<50> msg;
        msg.Copy(iMsg);
        CUtils::ShowMsg(msg);
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Accept faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }	
	}

//
// disconnection handler
//
void CSCOServerEngine::HandleDisconnectionCompleteL(TInt aErr)
	{
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("disconnect succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("disconnect faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }	
	}

//
// sending complete handler
//
void CSCOServerEngine::HandleSendCompleteL(TInt aErr)
	{
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Send succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Send faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }	
	}

//
// receiving complete handler
//
void CSCOServerEngine::HandleReceiveCompleteL(TInt aErr)
	{
    if(aErr == KErrNone)
        {
        TBuf<50> msg;
        msg.Copy(iMsg);
        CUtils::ShowMsg(msg);
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Receive faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }	
	}


//===============From MBluetoothPhysicalLinksNotifier==================
//
// creating physical link complete 
//
void CSCOServerEngine::HandleCreateConnectionCompleteL(TInt aErr)
	{
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Physical creating succeed"));
        
        //start listening
        TInt err = iSCOLink->AcceptConnection();
        if(err != KErrNone)
            {
            User::LeaveIfError(err);
            }
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Physical creating faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }	
	}

//
// physical link disconnected
//
void CSCOServerEngine::HandleDisconnectCompleteL(TInt aErr)
	{
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Physical disconnect succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Physical disconnect faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }	
	}

//
// all physical links disconnected
//
void CSCOServerEngine::HandleDisconnectAllCompleteL(TInt aErr)
	{
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Physical disconnect all succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Physical disconnect all faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }	
	}
